{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "eb9e6be6",
   "metadata": {},
   "source": [
    "|  字典的方法   | Description | 具体使用 |\n",
    "|  ----  | ----  | ----  |\n",
    "| .pop()  | 字典的删除 |1.先查询要删除的key是否存在 （避免keyError的报错）： 用 in 去查询 成员（key） 是否存在 2. 然后再删除：|\n",
    "| .popitem()  | 用于随机弹出字典中的一个 key-value 对 |随机删除\n",
    "|.setdefault() |设置默认值|1.如果key在字典中，则直接返回原有的值，不会修改值。2.如果key不在字典中，则先为该 key 设置默认的值，再返回该key对应的值|\n",
    "|.fromkeys() |给定的多个key创建字典|这些key对应的值默认都是None，也可以额外传入一个参数作为默认的值。\n",
    "|.clear()|清空所有值 |对一个字典执行 clear() 方法之后，该字典就会变成一个空字典|\n",
    "|.get()|根据key来获取值|使用方括号语法访问并不存在的 key 时，字典会引发KeyError错误；但如果使用 get() 方法访问不存在的 key，该方法会简单地返回 None，不会导致错误|\n",
    "|.copy()|复制字典|copy() 方法返回指定字典的副本|\n",
    "|.update|可使用一个字典所包含的key-value对来更新己有的字典|如果被更新的字典中己包含对应的key-value对,那么原有的值会被覆盖；如果被更新的字典中不包含对应的key-value对，则该key-value对被添加进去\n",
    "|.items()|以列表返回可遍历的(键, 值) 元组数组。|\n",
    "|.keys()|以列表返回一个字典所有的键|\n",
    "|.values()|返回一个视图对象|视图对象不是列表，不支持索引，可以使用 list() 来转换为列表；我们不能对视图对象进行任何的修改，因为字典的视图对象都是只读的。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d9294f8",
   "metadata": {},
   "source": [
    "### .pop（）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "42f86bcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# .pop()\n",
    "\n",
    "cars = {'BMW': 8.5, 'BENS': 8.3, 'AUDI': 7.9}\n",
    "\n",
    "if \"BMW\" in cars:\n",
    "   cars.pop('BMW')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c3badf7d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'BENS': 8.3, 'AUDI': 7.9}\n"
     ]
    }
   ],
   "source": [
    "print(cars)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c984b69",
   "metadata": {},
   "source": [
    "## .popitem（）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "68f83e69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu03', 'Jcak')"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu01\":\"Lili\",\n",
    "    \"stu02\":\"Mary\",\n",
    "    \"stu03\":\"Jcak\"\n",
    "    \n",
    "}\n",
    "\n",
    "\n",
    "stu_info.popitem()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f939268d",
   "metadata": {},
   "source": [
    "## .setdefault()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "158a476b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mike\n",
      "{'stu01': 'Lili', 'stu02': 'Mary', 'stu03': 'Jcak', 'stu00': 'Mike'}\n"
     ]
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu01\":\"Lili\",\n",
    "    \"stu02\":\"Mary\",\n",
    "    \"stu03\":\"Jcak\"\n",
    "    \n",
    "}\n",
    "\n",
    "print(stu_info.setdefault(\"stu00\",\"Mike\"))\n",
    "\n",
    "print(stu_info)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "4c59d4c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lili\n"
     ]
    }
   ],
   "source": [
    "print(stu_info.setdefault(\"stu01\",\"Lily\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a673f546",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu01': 'Lili', 'stu02': 'Mary', 'stu03': 'Jcak', 'stu00': 'Mike'}\n"
     ]
    }
   ],
   "source": [
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5fb90b3",
   "metadata": {},
   "source": [
    "##  .fromkeys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "b8490b6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': None, 'b': None}\n",
      "{13: None, 17: None}\n",
      "{13: 'good', 17: 'good'}\n"
     ]
    }
   ],
   "source": [
    "# 使用列表创建包含2个key的字典\n",
    "a_dict = dict.fromkeys(['a', 'b'])\n",
    "print(a_dict) \n",
    "\n",
    "# 使用元组创建包含2个key的字典\n",
    "b_dict = dict.fromkeys((13, 17))\n",
    "print(b_dict)\n",
    "\n",
    "# 使用元组创建包含2个key的字典，指定默认的值\n",
    "c_dict = dict.fromkeys((13, 17), 'good')\n",
    "print(c_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54ca91f7",
   "metadata": {},
   "source": [
    "## .clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "da2fbffc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu01': 'Lili', 'stu02': 'Mary', 'stu03': 'Jcak'}\n"
     ]
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu01\":\"Lili\",\n",
    "    \"stu02\":\"Mary\",\n",
    "    \"stu03\":\"Jcak\"\n",
    "    \n",
    "}\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "91376fa5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "stu_info.clear()\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec8a7227",
   "metadata": {},
   "source": [
    "## .get()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "9b0ebf2d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8.5\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "cars = {'BMW': 8.5, 'BENS': 8.3, 'AUDI': 7.9}\n",
    "# 获取'BMW'对应的value\n",
    "\n",
    "print(cars.get('BMW')) \n",
    "\n",
    "print(cars.get('PORSCHE'))\n",
    "\n",
    "# print(cars['PORSCHE']) # KeyError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "f782e2c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lili\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu01\":\"Lili\",\n",
    "    \"stu02\":\"Mary\",\n",
    "    \"stu03\":\"Jcak\"\n",
    "    }\n",
    "print(stu_info.get(\"stu01\"))\n",
    "\n",
    "print(stu_info.get(\"stu00\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55b9beb8",
   "metadata": {},
   "source": [
    "## .copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "a9678e03",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu01': 'Lili', 'stu02': 'Mary', 'stu03': 'Jcak'}\n"
     ]
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu01\":\"Lili\",\n",
    "    \"stu02\":\"Mary\",\n",
    "    \"stu03\":\"Jcak\"\n",
    "    }\n",
    "a = stu_info.copy()\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "091bc513",
   "metadata": {},
   "source": [
    "## .update()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "14fb47b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'BMW': 4.5, 'BENS': 8.3, 'AUDI': 7.9, 'PORSCHE': 9.3}\n"
     ]
    }
   ],
   "source": [
    "cars = {'BMW': 8.5, 'BENS': 8.3, 'AUDI': 7.9}\n",
    "cars.update({'BMW':4.5, 'PORSCHE': 9.3})\n",
    "print(cars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "ac87229c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu01': 'Lili', 'stu02': 'Mary', 'stu03': 88, 'stu04': 'Lily'}\n"
     ]
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu01\":\"Lili\",\n",
    "    \"stu02\":\"Mary\",\n",
    "    \"stu03\":\"Jcak\"\n",
    "    }\n",
    "stu_info.update({\"stu04\":\"Lily\",\"stu03\":88})\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1839d46c",
   "metadata": {},
   "source": [
    "## .items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "7e46d72a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict_items'>\n",
      "[('BMW', 8.5), ('BENS', 8.3), ('AUDI', 7.9)]\n"
     ]
    }
   ],
   "source": [
    "cars = {'BMW': 8.5, 'BENS': 8.3, 'AUDI': 7.9}\n",
    "\n",
    "# 获取字典所有的key-value对，返回一个dict_items对象\n",
    "a = cars.items()\n",
    "print(type(a)) \n",
    "\n",
    "# 将dict_items转换成列表\n",
    "print(list(a)) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "53cd998c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('brand', 'Porsche'), ('model', '911'), ('year', 1963)])\n"
     ]
    }
   ],
   "source": [
    "car = {\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "\n",
    "x = car.items()\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7934463",
   "metadata": {},
   "source": [
    "## .keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "f289f2d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict_keys'>\n",
      "['BMW', 'BENS', 'AUDI']\n",
      "BENS\n"
     ]
    }
   ],
   "source": [
    "# 获取字典所有的key，返回一个dict_keys对象\n",
    "b = cars.keys()\n",
    "print(type(b)) # <class 'dict_keys'>\n",
    "\n",
    "# 将dict_keys转换成列表\n",
    "print(list(b)) # ['BMW', 'BENS', 'AUDI']\n",
    "\n",
    "# 访问第2个key\n",
    "print(list(b)[1]) # 'BENS'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79f37465",
   "metadata": {},
   "source": [
    "## .values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "d798e353",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict_values'>\n",
      "8.3\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "# 获取字典所有的value，返回一个dict_values对象\n",
    "vals = cars.values()\n",
    "\n",
    "# 将dict_values转换成列表\n",
    "print(type(vals)) # [8.5, 8.3, 7.9]\n",
    "\n",
    "# 访问第2个value\n",
    "print(list(vals)[1]) # 8.3"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
